<?php
/**
 * Created by PhpStorm.
 * User: xuyiping
 * Date: 2019/1/25
 * Time: 16:27
 */

require 'config.php';

$e_name = 'e_linvo';    //交换机名
$q_name = 'q_linvo';     //队列名
$k_route = 'key_1';

//连接
$conn = new AMQPConnection($conn_args);

if(!$conn->connect()){
    die("Cannot connect to the broker \n");
}
//创建channel
$channel = new AMQPChannel($conn);
//创建交换机
$ex = new AMQPExchange($channel);

$ex->setName($q_name);
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE);

echo "Exchange Status:".$ex->declareExchange()."\n";

//创建队列

$q_list = new AMQPQueue($channel);
$q_list->setName($q_name);
$q_list->setFlags(AMQP_DURABLE);

//绑定交换机和队列与路由
echo "Queue Bind :".$q_list->bind($e_name,$k_route);

//阻塞模式接收消息
echo "Message:\n";
while (true){
    $q_list->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
}

$conn->disconnect();

function processMessage($envelope, $queue) {
    var_dump($envelope->getRoutingKey);
    $msg = $envelope->getBody();
    echo $msg."\n"; //处理消息
    $queue->ack($envelope->getDeliveryTag());
}
